<#include "../../ajaxPage.ftl" />
<#include "menuTree.ftl"/>
<!-- Modal -->
<div class="modal-dialog wide" style="width: 450px;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">分配菜单</h4>
        </div>
        <div class="modal-body" style="height: 400px; overflow-y: auto">
            <div id="errorMessage"><#include "../../message.ftl" /></div>
            <form class="form-horizontal" role="form" id="search_assign_menu_table" method="post"
                  action="${absoluteContextPath}/system/role/assignMenu">
                <input type="hidden" name="roleId" value="${roleId!''}"/>

                <div class="dd" id="nestable3">
                    <ol class="dd-list">
                    <#if assignMenuList?size gt 0>
                        <li class="dd-item dd3-item dd-active" data-id="1">
                            <div class="dd3-content">家康运营管理系统</div>
                            <@tree list=menuViewList assignList=assignMenuList/>
                        </li>
                    <#else>
                        <li class="dd-item dd3-item">
                            <div class="dd3-content">家康运营管理系统</div>
                            <@tree list=menuViewList assignList=assignMenuList/>
                        </li>
                    </#if>

                    </ol>
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            <button type="button" class="btn btn-primary" onclick="save();">保存</button>
        </div>
    </div>
</div>
<script>

    $(function () {
        var updateOutput = function (e) {
            var list = e.length ? e : $(e.target);
            output = list.data('output');
            if (window.JSON) {
                $(output).val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
            } else {
                output.val('JSON browser support required for this demo.');
            }
        };
        $('#nestable3').nestable({
            group: 1
        }).on('change', updateOutput);
        $('.dd3-content .controls a').tooltip();
        $('.dd3-content .dropdown-menu input').click(function (e) {
            e.preventDefault();
            e.stopPropagation();
        });

        $(".dd-list li").click(function (e) {
            if ($(e.target).attr("class") != "dd3-content") {
                return;
            }
            if (this.className.indexOf("dd-active") > 0) {
                $(this).removeClass("dd-active");
                $(this).find("li").removeClass("dd-active");
                $(this).find("input[name=ids]").attr("checked", false);
                $(this).find("li").find("input[name=ids]").attr("checked", false);
                var isCheckAll = true;
                $(this).siblings().each(function () {
                    if (this.className.indexOf("dd-active") > 0) {
                        isCheckAll = false;
                    }
                });
                if (isCheckAll) {
                    $(this).parent().parent().removeClass("dd-active");
                    $(this).parent().parent().find("input[name=ids]").attr("checked", false);
                }
            } else {
                $(this).addClass("dd-active");
                $(this).find("input[name=ids]").attr("checked", true);
                $(this).find("li").addClass("dd-active");
                $(this).find("li").find("input[name=ids]").attr("checked", true);
                $(this).parent().parent().addClass("dd-active");
                $(this).parent().parent().find("input[name=ids]").attr("checked", true);
            }
            e.preventDefault();
            e.stopPropagation();
        });

    });

    function save() {

        var ids = new Array();
        $(".dd-list li").each(function () {
            if ($(this).attr("data-id") != "0") {
                if (this.className.indexOf("dd-active") > 0) {
                    ids.push($(this).find("input[name=id]:first").val());
                }
            }
        });
        $("#" + "search_assign_menu_table").ajaxSubmit({
            url: '${absoluteContextPath}/system/role/assignMenu',
            data: {ids: ids},
            success: function (data) {
                data = JSON.parse(data);
                if (data.returnCode == 1) {
                    $.showMessage({parentId: "errorMessage", message: "信息保存成功！", type: "success", mode: "modal"});
                } else {
                    $.showMessage({parentId: "errorMessage", message: "<strong>错误信息</strong>：" + data.returnMsg, type: "success", mode: "modal"});
                }
            }
        });
    }
</script>